<template>
  <div>
    <el-col :span="24" style="height: 100%;">
      <div style="height: 100%;">
        <el-row style="height: 150px;padding-top: 50px" type=“flex” align=“middle”>
          <SearchBox></SearchBox>
        </el-row>
        <el-row class="mb8" >
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus"
                       size="mini" @click="handleAdd"
            >新增</el-button
            >
          </el-col>
        </el-row>
        <el-row>
          <el-table
            :max-height="700"
            v-loading="loading" :data="articleList"
            @selection-change="handleSelectionChange"
          >
            <el-table-column label="文章编号" prop="id"
                             :show-overflow-tooltip="true" width="180"
            />
            <el-table-column label="文章标题" prop="title"
                             :show-overflow-tooltip="true" width="250"
            />
            <el-table-column label="创建时间" prop="createTime"
                             :show-overflow-tooltip="true" width="250"
            />
            <el-table-column label="人数" prop="view"
                             :show-overflow-tooltip="true" width="150"
            />
            <el-table-column label="操作" align="center"
                             class-name="small-padding fixed-width"
            >
              <div slot-scope="scope" >
                <el-button size="mini" type="text"
                           icon="el-icon-edit" @click="handleUpdate(scope.row)"
                           v-if="scope.row.status !== 0"
                >修改</el-button
                >
                <el-button size="mini" type="text"
                           icon="el-icon-delete" @click="handleDelete(scope.row)"
                           v-if="scope.row.status !== 0"
                >删除</el-button
                >
                <el-button size="mini" type="text"
                           @click="handleUpdate(scope.row)"
                           v-if="scope.row.status !== 1"
                >详情</el-button
                >
              </div>
            </el-table-column>
          </el-table>
          <pagination v-show="total > 0"
                      :total="total" :page.sync="queryParams.pageNum"
                      :limit.sync="queryParams.pageSize" @pagination="getPage"
          />

          <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="100px">
              <el-form-item label="文章标题" prop="title">
                <el-input v-model="form.title" placeholder="请输入文章标题" />
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button type="primary" @click="submitForm">确 定</el-button>
              <el-button @click="cancel">取 消</el-button>
            </div>
          </el-dialog>
        </el-row>
      </div>
    </el-col>
  </div>
</template>
<script>
import Vertical from "@/layout/ManageMenu";
import Pagination from "@/components/Pagination";
import SearchBox from "@/views/reservation/search";
import {pageArticle,deleteArticle} from "@/api/article";
export default {
  name: 'ViewArticle',
  components: {
    SearchBox,
    Vertical,
    Pagination
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      scoreOpen: false,
      scores: {},

      // 日期范围
      dateRange: [],
      // 菜单列表
      menuOptions: [],
      // 部门列表
      deptOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        roleName: undefined,
        roleKey: undefined,
        status: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        startTime: [{ required: true, message: "开始时间不能为空", trigger: "blur" }],
        endTime: [{ required: true, message: "结束时间不能为空", trigger: "blur" }],
        location: [{ required: true, message: "地点不能为空", trigger: "blur" }],
      },
      defaultProps: {
        children: "children",
        label: "label",
      },

      statusList:[
        {
          id:0,
          label:"待进行"
        },
        {
          id:1,
          label:"已完成"
        },
      ],
    };
  },
  created() {
    // this.getList();
    this.getPage(this.queryParams);
  },
  methods: {
    getList() {
      this.loading = true;
      listConsult().then((response) => {
        if (response.success) {
          this.consultList = response.data;
          this.total = response.data.length;
          this.loading = false;
        }
      }).catch((e) => {
        _this.$message.error(e, "出现未知错误");
      });
    },
    //分页查询
    getPage(data) {
      this.loading = true;
      pageArticle(data)
        .then((response) => {
          if (response.success) {
            this.articleList = response.data.records;
            if(this.total===0){
              this.total = response.data.total;//重要，会决定有几个页码按钮
            }
            this.loading = false;
          }
        })
        .catch((e) => {
          _this.$message.error(e, "出现未知错误");
        });
    },

    // 新增按钮
    handleAdd(){
      this.reset();
      this.open=true;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 修改按钮
    handleUpdate(row) {
      this.reset();
      this.articleList.forEach((item) => {
        if (item.id === row.id) {
          this.form = JSON.parse(JSON.stringify(item));
          this.open = true;
          this.title = "修改文章";
        }
      });
    },
    handleDelete(row){
      deleteArticle({id:row.id})
        .then((response)=>{
          if(response.success){
            this.$alert("删除成功", {
              confirmButtonText: "确认",
              callback: (action) => {
                this.queryParams.pageNum=1
                this.getPage(this.queryParams)
                // console.log('请检查后重新输入')
              },
            });
          }
        })
    },
    // 表单重置
    reset() {
      this.form = {
        startTime: undefined,
        endTime:undefined,
        location:undefined,
      };
    },

  },
}
</script>

<style lang="scss" scoped>
.auto-generate-form {
  padding-top: 50px;
  padding-right: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
</style>
